import base64
import datetime

from Crypto import Random
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA

privateKey = b'-----BEGIN RSA PRIVATE KEY-----\nMIICWwIBAAKBgQCpFjOt+HXq5JAGMSKJU+9SjQTH+WOVrhKWENDFfMT35uc5Gpxd' \
             b'\nrWKQ+D9fn/96bwC0vxZ8svFi8MglELmukXRn6tt4wgnFTapdPgZi4JeipdEXu7X5' \
             b'\n3v9lMqnDeUVue8kQDPhjx8wf57MayvRCRDTrNTQe08xXgYjJNBZlMAYsdwIDAQAB\nAoGAB43mYS9olUYA6UXotRn650VFoWmS' \
             b'+Wz9h5bYvPoGEs1OJ075DXZhoPh2XF9Z\nmq666VhgONIF1tXLG1zT7uiwuqE1jGlsnnWPmkMp/J+7EyyqQZ1We1PDXgn5NWpj' \
             b'\nYsBXwcNNIM69wDTVHjva/d5zBcIZhZGW80NCDwwSJzPk5YECQQDFPTTdH4XPb/LX\nXNZBmkhbTl' \
             b'+ZcFUhefAXaeOr0P9UZThTmKswWKPjxGnm05RXnxcI7O1myDptFr6b\nlFt6XH1lAkEA23Xnp/jyXNLpda3Duktv' \
             b'/zG7qqg1VLepGTeIwX56BOXBxg+fxb23\n8Ufz9Buh6E+D/Mc42QlI884q7yKgQ0siqwJAeKwZy6+djmxPhrOl4j0iR7qLgrTX' \
             b'\nYMPZwHlocZZMoLXM4FhnZAzXOGLS6AZKm9EZprxOArQ+Cr//nfAGh7ZOgQJAFoGb\n9UT5jf1vRctBNFzCKpiuF01S0tJW' \
             b'/CgO1VEkMFZjre0yRHQ1ZgAsC4+95fYpogXI\nVbG3EpHFI2sYpm4RKwJAHfFOUmazj9EakcG4+T3FB/IuYITsm0NHtBaXlR9JTeU3' \
             b'\nz7SI9tZofzbDcbQJ8ESFwKSV02XxUnFzqs1pOWTxSw==\n-----END RSA PRIVATE KEY-----'

publicKey = b'-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpFjOt+HXq5JAGMSKJU+9SjQTH\n' \
            b'+WOVrhKWENDFfMT35uc5GpxdrWKQ+D9fn/96bwC0vxZ8svFi8MglELmukXRn6tt4' \
            b'\nwgnFTapdPgZi4JeipdEXu7X53v9lMqnDeUVue8kQDPhjx8wf57MayvRCRDTrNTQe\n08xXgYjJNBZlMAYsdwIDAQAB\n-----END ' \
            b'PUBLIC KEY-----'

# # 伪随机数生成器
random_generator = Random.new().read


def rsa_encrypt(data_str):
    # data_str:需要加密的明文文本，公钥加密，私钥解密
    # 加密过程
    # 1：加载公钥
    public_key = RSA.import_key(publicKey)  # 加载公钥key1
    # 加密
    cipher_rsa = PKCS1_v1_5.new(public_key)
    cipher_text = base64.b64encode(cipher_rsa.encrypt(data_str.encode('utf-8')))  # cipher_text 即为加密后的密文
    # 打印加密结果
    # Python3的字符串的编码语言用的是unicode编码，由于Python的字符串类型是str,如果直接打印出来print(cipher_text)，会是b'字符串'，解决方案是后面添加decode('utf-8')
    return cipher_text.decode('utf-8')


def rsa_decrypt(encrypt_data):
    # 读取私钥
    private_key = RSA.import_key(privateKey)  # 加载私钥key2
    # 解密
    cipher_rsa = PKCS1_v1_5.new(private_key)
    text = cipher_rsa.decrypt(base64.b64decode(encrypt_data), random_generator)
    # 打印解密结果
    # Python3的字符串的编码语言用的是unicode编码，由于Python的字符串类型是str,如果直接打印出来print(text)，会是b'字符串'，解决方案是后面添加decode('utf-8')
    return text.decode('utf-8')


if __name__ == '__main__':
    # 获取当前时间
    now = datetime.datetime.now()
    # 计算三个月后的时间
    three_months_later = now + datetime.timedelta(days=12 * 30)
    # 获取时间戳
    timestamp = str(int(three_months_later.timestamp()))
    en = rsa_encrypt(timestamp)
    print(en)
    de = rsa_decrypt(en)
    print(de)

    # # rsa算法生成秘钥对实例对象：1024是秘钥的长度
    # rsa = RSA.generate(1024, random_generator)
    #
    # # 生成私钥并保存到pem文件，如果知道秘钥值key3也可以不保存，拼接上begin/end标识后加载直接用，
    # # 如key3 = '-----BEGIN PUBLIC KEY-----\n' + key3 + '\n' + '-----END PUBLIC KEY-----'
    # private_pem = rsa.exportKey()
    # print(private_pem)
    # with open("private.pem", "wb") as f:  # wb,以二进制格式写入文件
    #     f.write(private_pem)
    #
    # # 生成公钥并保存到pem文件，当然也可以不保存直接用,即key1
    # public_pem = rsa.publickey().exportKey()
    # print(public_pem)
    # with open("public.pem", "wb") as f:  # wb,以二进制格式写入文件
    #     f.write(public_pem)
